﻿using FastMvc;
using FastMvc.Auth;
using FastMvc.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using JQ.SAAS.Entity;
using JQ.SAAS.DTO.OrgMenuSet;
using JQ.SAAS.IService;

namespace JQ.SAAS.WebServer.Controllers
{
    /// <summary>
    /// 机构字典设置
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [PermissionCheck("Admin.Sys.MenuSet", "菜单模板管理")]
    public class SysMenuSetController: BaseController
    {
        private ISysMenuSetService _setService;

        public SysMenuSetController(ISysMenuSetService setService)
        {
            _setService = setService;
        }

        /// <summary>
        /// 获取机构菜单设置树
        /// </summary>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<BaseResult<List<MenuTreeNodeDto>>> GetOrgSetTree()
        {
            var datas = await _setService.GetOrgSetTree();

            return await SetResultNotNull(datas);
        }

        /// <summary>
        /// 查询可用的页面菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("pageMenus")]
        public async Task<BaseResult<List<PageMenuDto>>> GetOrgPageMenus([FromQuery]PageMenuQueryDto query)
        {
            List<PageMenuDto> datas = await _setService.GetOrgPageMenus(query);

            return await SetResultNotNull(datas);
        }

        /// <summary>
        /// 添加一个菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost("")]
        public async Task<BaseResult<MenuTreeNodeDto>> AddTreeNode([FromBody]MenuSetCreateDto orgMenuSetCreateDto)
        {
            MenuTreeNodeDto data = await _setService.AddTreeNode(orgMenuSetCreateDto);

            return await SetResultNotNull(data);
        }

        /// <summary>
        /// 菜单节点编辑
        /// </summary>
        /// <returns></returns>
        [HttpPut("{sId}")]
        public async Task<BaseResult<MenuTreeNodeDto>> UpdateTreeNode(string sId, [FromBody] MenuSetUpdateDto orgMenuSetUpdateDto)
        {
            MenuTreeNodeDto data = await _setService.UpdateTreeNode(sId, orgMenuSetUpdateDto);

            return await SetResultNotNull(data);
        }

        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{sId}")]
        public async Task<BaseResult> DeleteTreeNode(string sId)
        {
            await _setService.DeleteTreeNode(sId);

            return BaseResult.Success("删除成功！");
        }
    }
}
